package com.github.livebase.util.base;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName ArraysUtil
 * @description:
 * @author: biao532
 * @create: 2023-12-19 14:24
 * @Version 1.0
 **/
public class ArraysUtil {
    public static<T> boolean isEmpty(T[] arrays) {
        return arrays == null || arrays.length == 0;
    }
    public static<T> void foreach(T[] array, CollectionsUtil.SimpleVisitor<T> simpleVisitor) {
        ArraysUtil.foreach(array, (c)-> {
            simpleVisitor.visit(c);
            return true;
        });
    }
    public static<T> void sort(T[] array, Comparator<T> comparator) {
        for (int i = 0, l = array.length; i < l; i++) {
            for (int j = i + 1; j < l; j++) {
                if ( comparator.compare(array[i], array[j]) > 0) {
                    T tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    public static<T> void foreach(T[] array, CollectionsUtil.Visitor<T> visitor) {
        if (array == null) {
            return;
        }
        for (int i = 0, l = array.length; i < l; i++) {
            T c = array[i];
            visitor.visit(i++, c);
        }
    }
    private static<T> T[] createArray(T[] array) {
        Class<?> type = array.getClass().getComponentType();
        return (T[])Array.newInstance(type, array.length);
    }
    private static<T> void foreach(T[] array, CollectionsUtil.Filter<T> filter) {
        if (array == null) {
            return;
        }
        for (T c : array) {
            if (!filter.filter(c)) {
                return;
            }
        }
    }
}
